﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;

namespace CodeJam.StoreCredit
{
    class Program
    {
        static void Main(string[] args)
        {
            args = args.Length == 0 ? new string[] { "sample.txt" } : args;
            List<TestCase> testCases = TestCaseReader.ReadTestCases(args);
            if (testCases != null)
            {
                StoreCredits(testCases);
                TestCaseReader.PrintTestCases(testCases);
            }

            Console.Read();
        }

        private static void StoreCredits(List<TestCase> testCases)
        {
            foreach (TestCase item in testCases)
            {
                StoreCredit(item);
            }
        }

        private static void StoreCredit(TestCase item)
        {
            bool found = false;
            int parent = 0;
            int son = 0;

            for (int i = 0; i < item.Items.Count; i++)
            {
                parent = item.Items[i];

                for (int j = 0; j < item.Items.Count; j++)
                {
                    son = item.Items[j];

                    if (i != j)
                    {
                        if (parent + son == item.Credit)
                        {
                            item.Cart = string.Format("{0} {1}", i + 1, j + 1);
                            found = true;
                            break;
                        }
                    }
                }

                if (found)
                    break;
            }
        }        
    }

    public class TestCase
    {
        public int NumberItems { get; set; }
        public int Credit { get; set; }
        public string Cart { get; set; }
        public List<int> Items { get; set; }
    }

    public class TestCaseReader
    {
        public static List<TestCase> ReadTestCases(string[] args)
        {
            if (args.Length < 1)
            {
                Console.WriteLine("must provide a test file.");
                return null;
            }

            string filename = args[0].ToString();

            if (!File.Exists(filename))
            {
                Console.WriteLine("test file: {0} doesn't exist.", filename);
                return null;
            }

            TextReader tr = new StreamReader(filename);
            List<TestCase> testcases = ReadTestCases(tr);
            
            tr.Close();

            return testcases;
        }

        private static List<TestCase> ReadTestCases(TextReader tr)
        {
            List<TestCase> testCases = new List<TestCase>();
            int numberTestCases = 0;
            int.TryParse(tr.ReadLine(), out numberTestCases);
            TestCase t;
            string[] items;

            for (int i = 0; i < numberTestCases; i++)
            {
                t = new TestCase();
                t.Items = new List<int>();
                t.Credit = int.Parse(tr.ReadLine());
                t.NumberItems = int.Parse(tr.ReadLine());
                items = tr.ReadLine().Split(' ');
                for (int j = 0; j < t.NumberItems; j++)
                {
                    t.Items.Add(int.Parse(items[j]));
                }
                testCases.Add(t);
            }

            return testCases;
        }

        public static void PrintTestCases(List<TestCase> testCases)
        {
            TextWriter tw = new StreamWriter("testOutput.txt");
            int i = 1;
            string line = string.Empty;

            Console.WriteLine("Test Cases:");
            Console.WriteLine();
            foreach (TestCase item in testCases)
            {
                line = string.Format("Case #{0}: {1}", i, item.Cart);
                Console.WriteLine(line);
                tw.WriteLine(line);
                //PrintTestCase(item);
                i++;
            }

            tw.Close();
        }

        private static void PrintTestCase(TestCase item)
        {
            Console.WriteLine("{0}", item.Cart);
            //foreach (FieldInfo item in item.GetType().GetFields())
            //{
            //    Console.WriteLine("{0}", case);
            //}
        }
    }
}
